﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DBAccess;
using SSPharma.Common;
using SSPharma.Model;
using System.Data.Common;
using System.Transactions;
using SSPharma.Biz.Common;

namespace SSPharma.Biz.HeThong
{
    class BizResponseMenu
    {
        /// <summary>
        /// User: TAMNN 07/08/2014
        /// lấy ResponseMenu theo mã ResponseMenu
        /// </summary>
        /// <param name="Ma"></param>
        /// <returns></returns>
        public DataTable ft_GetDataResponseMenuTheoMa(string Ma)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append(" SELECT a.*,Choose = convert(bit,1),MaMenuHienThi = b.MaMenu,TenMenuHienThi = b.TenMenu FROM HT_Response_Menu a with(nolock)");
            strSQL.Append(" LEFT OUTER JOIN HT_Menu b with(nolock) ON a.MaMenu = b.MaMenu");
            strSQL.Append(" where a.MaResponse = @Ma");
            strSQL.Append(" UNION ALL");
            strSQL.Append(" SELECT d.*,Choose = convert(bit,0), MaMenuHienThi = c.MaMenu,TenMenuHienThi = c.TenMenu FROM HT_Menu c with(nolock)");
            strSQL.Append(" LEFT OUTER JOIN HT_Response_Menu d with(nolock) ON d.MaMenu = c.MaMenu");
            strSQL.Append(" WHERE isnull(c.MaMenuCha,'') ='' AND ISNULL(d.MaMenu,'') = ''");
            SQLServer.Param[] Params = new SQLServer.Param[1];
            Params[0] = new SQLServer.Param("@Ma", Ma);
            return Global.DataFactory.GetDataTable(strSQL.ToString(), Params);
        }
        /// <summary>
        /// User: TAMNN 07/08/2014
        /// kiểm tra mã Response
        /// </summary>
        /// <param name="Ma"></param>
        /// <returns></returns>
        public DataTable ft_KiemTraResponseTheoMa(string Ma)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append(" SELECT MaResponse,TenResponse FROM HT_DM_Response");
            strSQL.Append(" where MaResponse = @Ma");
            SQLServer.Param[] Params = new SQLServer.Param[1];
            Params[0] = new SQLServer.Param("@Ma", Ma);
            return Global.DataFactory.GetDataTable(strSQL.ToString(), Params);
        }
        /// <summary>
        /// User: TAMNN 07/08/2014
        /// Lay cau truc de luu du lieu xong database
        /// </summary>
        /// <returns></returns>
        public DataSet fs_LayCauTrucTable()
        {
            DataSet dsdata = new DataSet();
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append(" SELECT * FROM HT_Response_Menu");
            strSQL.Append(" where 1=2");
            DataTable dt_ResponseMenu = Global.DataFactory.GetDataTable(strSQL.ToString());
            dt_ResponseMenu.TableName = "dt_ResponseMenu";
            dsdata.Tables.Add(dt_ResponseMenu);
            return dsdata;
        }
        /// <summary>
        /// User: TAMNN 07/08/2014
        /// Cap nhat response
        /// </summary>
        /// <param name="dt"></param>
        public void fg_UpdateResponseMenu(DataTable dt)
        {
            try
            {
                string query_ResponseMenu = "Select * from HT_Response_Menu where 1 = 2";
                using (TransactionScope scope = new TransactionScope())
                {
                    Global.DataFactory.RunSaveData(dt, query_ResponseMenu);
                    scope.Complete();
                    scope.Dispose();
                }
            }
            catch (Exception ex)
            {
                throw new System.Configuration.Provider.ProviderException(ex.Message);
            }
        }
        /// <summary>
        /// User: TAMNN 23/07/2014
        /// Mục đích: lấy danh sách theo danh mục
        /// </summary>
        /// <returns></returns>
        public DataTable ft_GetListdata(IList<string> list, string Orderby)
        {
            string str = string.Join(",", list.ToArray());
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append(" SELECT a.MaResponse,a.TenResponse,a.MaMenu,a.TenMenu,b.TenForm,b.DirectoryViewModel,b.ViewModel,b.DirectoryViews,b.Views,a.GhiChu,a.TinhTrang");
            strSQL.Append(" FROM  HT_Response_Menu a LEFT OUTER JOIN HT_DM_MenuForm b ON a.MaMenu = b.MaMenu");
            strSQL.Append(!string.IsNullOrEmpty(Orderby) ? (" order by " + Orderby) : "");
            return Global.DataFactory.GetDataTable(strSQL.ToString());
        }

        /// <summary>
        /// User: TAMNN 07/08/2014
        /// Mục đích: Lấy cấu trúc các cột,chiều dài,thứ tự... của các cột cần hiển thi
        /// </summary>
        /// <param name="_tenform">Tên bảng của danh mục đó trong database</param>
        /// <returns></returns>
        public DataTable ft_DSDinhDangLuoi(string _tenform)
        {
            string strQuery = "Select * from HT_DinhDangHTDM where TenBang =@TenBang order by STT";
            SQLServer.Param[] Params = new SQLServer.Param[1];
            Params[0] = new SQLServer.Param("@TenBang", _tenform);
            return Global.DataFactory.GetDataTable(strQuery.ToString(), Params);
        }
        /// <summary>
        /// User: TAMNN 07/08/2014
        /// Mục đích: Xóa dữ liệu
        /// </summary>
        /// <param name="_tenbang">tên bảng của danh mục đó trong database</param>
        /// <param name="ListKhoaChinh">List khóa chính</param>
        /// <param name="Values">List giá trị tương ứng với khóa chính đó</param>
        /// <returns></returns>
        public string fs_DeleteData( IList<string> Values)
        {
            //string strSQL = string.Empty;
            //strSQL += " delete " + _tenbang;
            //strSQL += " where ";
            //for (int i = 0; i < ListKhoaChinh.Count; i++)
            //    strSQL += ListKhoaChinh[i] + " = '" + Values[i] + "' and ";
            //strSQL = strSQL.Substring(0, strSQL.LastIndexOf("and"));
            //using (DbDataReader dataReader = Global.DataFactory.Reader(strSQL.ToString(), null))
            //{
            //    if (dataReader.HasRows)
            //    {
            //        return dataReader.GetSchemaTable().Rows[0][0].ToString();
            //    }
            //}
            return "Xóa thành Công";
        }
    }
}
